package com.ruoyi.web.controller.spoken;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.UniqueCodeUtils;
import com.ruoyi.system.domain.SpokenOrder;
import com.ruoyi.system.domain.SpokenTeacher;
import com.ruoyi.system.service.ISpokenOrderService;
import com.ruoyi.system.service.ISpokenTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;

@RequestMapping("/spoken/order")
@Api(description = "订单管理")
@RestController
public class OrderController extends BaseController {

    @Autowired
    ISpokenOrderService orderService;

    @ApiOperation("创建预支付订单")
    @PostMapping
    public AjaxResult add(@RequestBody SpokenOrder order){
        orderService.insert(order);
        Long orderId = order.getOrderId();
        return success(orderId);
    }

    @ApiOperation("查询订单信息")
    @GetMapping("/{orderId}")
    public AjaxResult getById(@PathVariable Long orderId){
        Map<String, Object> result = orderService.selectById(orderId);
        return success(result);
    }

    @ApiOperation("下单购买")
    @GetMapping("/toPay/{orderId}")
    public AjaxResult toPay(@PathVariable Long orderId){
        return success(orderService.genPayParam(orderId));
    }

    @ApiOperation("支付回调")
    @PostMapping("/payCallback")
    public AjaxResult payCallback(HttpServletRequest request, HttpServletResponse response){
        return orderService.payCallback(request);
    }

    @ApiOperation("订单列表")
    @GetMapping("/list")
    public TableDataInfo list(SpokenOrder order){
        String userType = SecurityUtils.getLoginUser().getUser().getUserType();
        // 小程序用户查询订单列表
        if("01".equals(userType)){
            order.setUserId(SecurityUtils.getUserId());
        }
        startPage();
        return getDataTable(orderService.selectList(order));
    }

    @ApiOperation("取消订单")
    @GetMapping("/cancel/{orderId}")
    public AjaxResult cancel(@PathVariable Long orderId){
        return toAjax(orderService.cancelOrder(orderId));
    }

    @ApiOperation("删除订单")
    @DeleteMapping("/{orderId}")
    public AjaxResult remove(@PathVariable Long orderId){
        SpokenOrder spokenOrder = new SpokenOrder();
        spokenOrder.setOrderId(orderId);
        spokenOrder.setDelFlag(0);
        return success(orderService.update(spokenOrder));
    }
}
